//
//  ViewController.m
//  BlackCool
//
//  Created by cocoazxj@163.com on 15/4/29.
//  Copyright (c) 2015年 handcool. All rights reserved.
//

#import "ViewController.h"
#import "VideoModel.h"
#import "VideoTableViewCell.h"
#import <MediaPlayer/MediaPlayer.h>
#import "VideoViewController.h"
#import "MobileInformation.h"


#define VIDEO_URL (@"http://192.168.0.77:8080/MJServer/video")

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) NSMutableArray *mArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self getData];
    NSLog(@"%f,%f,%@,%@",SCREEN_W,SCREEN_H,SYSTEM_V,SYSTEM_M);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -
#pragma mark XML
- (void)usedXML
{
    NSURL *url = [NSURL URLWithString:VIDEO_URL];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.timeoutInterval = 15.0;
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse* response, NSData* data, NSError* connectionError){
        NSLog(@"controller->view\n method->getdata\n dataresponse->%@\n",response);
        if(data)
        {
//            self.mArray = mArr;
//            [self.tv_main reloadData];
        }
        else
        {
            
        }
    }];

}

#pragma mark -
#pragma mark Get Data
- (void)getData
{
    // 1.获取URL
//    NSString *uString = @"http://192.168.0.77:8080/MJServer/video/";
    NSURL *url = [NSURL URLWithString:VIDEO_URL];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.timeoutInterval = 15.0;
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse* response, NSData* data, NSError* connectionError){
        NSLog(@"controller->view\n method->getdata\n dataresponse->%@\n",response);
        if(data)
        {
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
            NSArray *array = dict[@"videos"];
            
            NSMutableArray *mArr = [NSMutableArray array];
            for(NSDictionary *d in array)
            {
                NSLog(@"%@",[VideoModel videoModelWithDict:d]);
                [mArr addObject:[VideoModel videoModelWithDict:d]];
            }
            self.mArray = mArr;
            NSLog(@"======1数据个数=======%lu",(unsigned long)self.mArray.count);
            [self.tv_main reloadData];
        }
        else
        {
            
        }
    }];
}

#pragma mark -
#pragma mark UITableView Delegate Method
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"======2数据个数=======%lu",(unsigned long)self.mArray.count);
    return self.mArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    VideoTableViewCell *cell = [VideoTableViewCell cellWithTable:tableView];
    cell.videoModel = self.mArray[indexPath.row];
    NSLog(@"======图片缓存个数=======%lu",(unsigned long)[SDImageCache sharedImageCache].getDiskCount);
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    VideoModel *m = self.mArray[indexPath.row];
    VideoViewController *mp = [[VideoViewController alloc] initWithContentURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://192.168.0.77:8080/MJServer/%@",m.url]]];
    [self presentMoviePlayerViewControllerAnimated:mp];
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}
@end
